package praca.framework;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import praca.interfejsy.Game;
import praca.interfejsy.Graphics;
import praca.interfejsy.Texture;
import android.graphics.Bitmap;
import android.graphics.Color;

public class GraphicsGL implements Graphics{

	GL10 gl;
	Random rand = new Random();
	Game game;
	ShortBuffer indices;
	FloatBuffer vertices;
	Bitmap bitmap;
	private float r = 1;
	private float g = 1;
	private float b = 1;
	private float a = 1;
	Vertices vert = null;
	
	
	public GL10 getGL(){
		return gl;
	}
	
	public GraphicsGL(Game gra){
		game = gra;
	}
	
	public void enableStateTexture(){
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	}
	
	@Override
	public void drawTexture(Texture texture, Vertices position) {
		
		if(!position.isBitmapVerex()){
			throw new RuntimeException();
		}
		
		
		vertices = position.getVertices();
		indices = position.getIndices();
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		
		vertices.position(0);
		gl.glVertexPointer(2, GL10.GL_FLOAT, 4, vertices);
		vertices.position(2);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 4, vertices);
		
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture.getTextureId());
		vertices.position(0);
		gl.glVertexPointer(2, GL10.GL_FLOAT, 16, vertices);
		vertices.position(2);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 16, vertices);
		gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, indices);
		
		gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	}

	

	@Override
	public void clearScreen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void drawLine() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void drawRandomColor(){
		gl.glClearColor(rand.nextFloat(), rand.nextFloat(),
                rand.nextFloat(), 1);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	}
	@Override
	public void drawColor(float r, float g, float b){
		gl.glClearColor(r, g,
                b, 1);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	}
	
	@Override
	public void setGL(GL10 gl) {
		this.gl = gl;
		
	}
	@Override
	public void rotateTexture(float angle){
		gl.glRotatef(angle, 0, 0, 1);
	}
	
	@Override
	public void scaleTexture(float x, float y){
		gl.glScalef(x, y, 1);
	}
	
	public void reload(){
		
	}
	
	@Override
	public void setDefaultColor(float r, float g, float b, float alpha){
		this.r = r;
		this.g = g;
		this.b = b;
		this.a = a;
		gl.glColor4f(r, g, b, a);
	}


	@Override
	public void drawRect(Vertices vert) {
		//drawRandomColor();

		gl.glColor4f(r, g, b, a);
		
		 
	    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
	    if(vert.isBitmapVerex())
		gl.glVertexPointer(2 /*kazdy wierzcholek z 2 wspolrzednych*/
       		 , GL10.GL_FLOAT, 16 /*Jak daleko jedna od drugiej */, vert.floatBuffer);
	    else
	    	gl.glVertexPointer(2 /*kazdy wierzcholek z 2 wspolrzednych*/
	          		 , GL10.GL_FLOAT, 0 /*Jak daleko jedna od drugiej */, vert.floatBuffer);
		gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, vert.shortBuffer);
	}
	
	

	@Override
	public void drawRect(int x1, int y1, int x2, int y2, Color color) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void drawImage(Bitmap bitmap, Vertices position) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public GL10 getGl() {
		// TODO Auto-generated method stub
		return gl;
	}

	@Override
	public void clearTransformations(){
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

	@Override
	public void moveTexture(float x, float y) {
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glTranslatef(x, y, 0);
		
	}



	@Override
	public void setTextureTransparentBackground() {
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
	}

	@Override
	public void setTransparency(float alpha){
		gl.glColor4f(1, 1, 1, alpha);
	}

	
	private void drawTexture(Texture texture, int width, int height) {
		
		if(vert == null){
			vert = new Vertices(width,height, true);
		
			vertices = vert.getVertices();
			indices = vert.getIndices();
		}
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		
		vertices.position(0);
		gl.glVertexPointer(2, GL10.GL_FLOAT, 4, vertices);
		vertices.position(2);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 4, vertices);
		
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture.getTextureId());
		vertices.position(0);
		gl.glVertexPointer(2, GL10.GL_FLOAT, 16, vertices);
		vertices.position(2);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 16, vertices);
		gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, indices);
		
	}
	public void drawTriangle(Vertices vert) {
		FloatBuffer vertices = vert.getVertices(); 
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertices);
        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
		
	}
	@Override
	public void drawTexture(TextureAtlas textureAtlas, Vertices pos, int nrSprites) {
		vertices = pos.getVertices();
		indices = pos.getIndices();
		
		vertices.position(0);
		gl.glVertexPointer(2, GL10.GL_FLOAT, 4, vertices);
		vertices.position(2);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 4, vertices);
		
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textureAtlas.getTextureId());
		vertices.position(0);
		gl.glVertexPointer(2, GL10.GL_FLOAT, 16, vertices);
		vertices.position(2);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 16, vertices);
		gl.glDrawElements(GL10.GL_TRIANGLES, nrSprites * 6, GL10.GL_UNSIGNED_SHORT, indices);
		
	}

	@Override
	public void enableDrawingStates() {
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		
	}

}
